package cn.edu.gdut.csq.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

import cn.edu.gdut.csq.entity.vo.Setting;
import cn.edu.gdut.csq.util.JsonUtils;
import cn.edu.gdut.csq.util.PoolJedisClient;

@Service
public class SettingsService {

	@Autowired
	private PoolJedisClient jedis;

	public Setting getSettings() {

		String jediskeys = jedis.get("keywords");
		List<String> jsonArrayToList = JsonUtils.jsonArrayToList(jediskeys);
		// String citys = jedis.get("citys");

		Setting setting = new Setting();
		setting.setKeywords(jsonArrayToList);

		return setting;
	}

	public boolean updateSettings(String keywords, String citys) {
		// TODO Auto-generated method stub

		String[] split = keywords.split(",|，");
		for (String string : split) {

		}
		ObjectMapper objectMapper = new ObjectMapper();
		ObjectWriter writerFor = objectMapper
				.writerFor(new TypeReference<String[]>() {
				});
		try {
			String writeValueAsString = writerFor.writeValueAsString(split);
			// System.out.println(writeValueAsString);
			jedis.set("keywords", writeValueAsString);

		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return true;
	}

}
